﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.Logic;
using System.Collections.Generic;
using Cosmetic.DataEntity;
using Cosmetic;

namespace WebApp.Controls
{
    public partial class NewProductsForPoint : System.Web.UI.UserControl
    {
        private CategoryManager m_CategoryManager = new CategoryManager();
        private ProductManager m_ProductManager = new ProductManager();

        private List<ProductInfo> theProductList;

        public int PointCoefficient = Convert.ToInt32(ConfigurationManager.AppSettings["PointCoefficient"]);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //this.CategoryToCategoryAndProduct.CategoryID = m_CID;
                BindProduct();
            }
        }

        private void BindProduct()
        {
            theProductList = this.m_ProductManager.GetPointProductInfo().Take(4).ToList(); //this.txtProductName.Text.Trim());


            this.lvProduct.DataSource = theProductList;
            this.lvProduct.DataBind();
        }

        protected void lvProdust_ItemCommand(object sender, ListViewCommandEventArgs e)
        {

            if (Session["SignInUser"] == null)
            {
                WebGeneric.WebMessageBox("您尚未登陆，请先登陆！", this.Page);
                this.Response.Redirect(String.Format("~/Login.aspx?R={0}", this.Request.Url.PathAndQuery));
            }
            User theUser = (User)Session["SignInUser"];
            if (e.CommandName.Equals("OnChange"))
            {
                Cosmetic.DataEntity.ProductInfo theProductInfo = m_ProductManager.GetProductInfoById(new Guid(e.CommandArgument.ToString()));

                int thePointCoefficient = Convert.ToInt32(ConfigurationManager.AppSettings["PointCoefficient"].ToString());
                decimal theIntergral = theProductInfo.网络零售价 * thePointCoefficient;

                if (theUser.Integral < theIntergral)
                {
                    WebGeneric.WebMessageBox("对不起，您的积分不够！", this.Page);
                    return;
                }
                Session["pointcart"] = new Hashtable();
                ((Hashtable)Session["pointcart"]).Add(theProductInfo.ProductId.ToString(), "1");

                Order theOrder = new Order();
                theOrder.Price = 0;
                theOrder.Intergral = 0 - theIntergral;

                Session["Order"] = theOrder;
                Response.Redirect("~/Shopping/ConfirmUserInfo.aspx");
            }
        }

        protected void lvProduct_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            //HyperLink hl = e.Item.FindControl("HyperLink1") as HyperLink;
            //if (hl.Text.Length > 6)
            //{
            //    hl.Text = hl.Text.Substring(0,6);
            //}
        }
    }
}